<?php

function socketsSendXML($host, $port, $xml){

	set_time_limit(0);

	/* Change to your Java Server Port */
	$sport = $port;
	
	/* Get the IP address for the target host. */
	$shost = $host;
	
	/* Create a TCP/IP socket. */
	$socket = socket_create(AF_INET,SOCK_STREAM, SOL_TCP);
	if ($socket === false) {
		echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<BR>";
	}

	echo "Attempting to connect to " . $shost . " on port " . $sport . " <br> ";
	$result = socket_connect($socket, $shost, $sport);
	if ($result === false) {
		echo "socket_connect() failed.\nReason: (" . $result . ") " . socket_strerror(socket_last_error($socket)) . "<BR>";
	}

	$in = $xml . "\n";
	$in .= chr(13) . chr(10); //End Of Java Transmission 

	$out = '';
	$response = '';

	echo "Sending Data Transfer <br> ";
	socket_write($socket, $in, strlen($in));

	echo "Reading response:\n\n:";
	try {
		while ($out = @socket_read($socket,2048, PHP_NORMAL_READ)) {
			$response .= $out;
		}

		echo $response;

	} catch (Exception $e) {
	    echo "Caught exception: " .  $e->getMessage() . "\n";
	}

		echo "<br>Closing socket!";
		socket_close($socket);
		return $response;
}

?>


